package com.qing.common.util

import android.app.Application
import android.content.Context

object CommonUtils {

    fun dip2px(context: Context = getAppContext(), value: Float): Int {
        val density = context.resources.displayMetrics.density
        return (value * density + 0.5f).toInt()
    }

    fun sp2px(context: Context = getAppContext(), value: Float): Int {
        val scaledDensity = context.resources.displayMetrics.scaledDensity
        return (value * scaledDensity + 0.5f).toInt()
    }

    fun getScreenWidth(context: Context = getAppContext()): Int {
        return context.resources.displayMetrics.widthPixels
    }

    fun getScreenHeight(context: Context = getAppContext()): Int {
        return context.resources.displayMetrics.heightPixels
    }

    fun getAppContext(): Context {
        return Class.forName("android.app.ActivityThread")
            .getMethod("currentApplication").invoke(null) as Application
    }
}